home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-10-15 | 7.1 KB | 233 lines | [TEXT/MPS ] |
- /*
- * File: Service.cp
- *
- * Contains: xxx put contents here xxx
- *
- * Written by: Rick Violet
- *
- * Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
- *
- * Change History (most recent first):
- *
- * <5+> 11/19/92 RV
- * 11/18/92 RV xxx put comment here xxx
- *
- * To Do:
- */
-
- #ifndef __Service__
- #include "Service.h"
- #endif
-
- #ifndef __Application__
- #include "Application.h"
- #endif
-
- #ifndef __RequestDispatcher__
- #include "RequestDispatcher.h"
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Global Variables
- //—————————————————————————————————————————————————————————————————————————————————————
- extern RequestDispatcher* gTheRequestDispatcher;
- extern ThreadID gRequestDispatcherThreadID;
- extern Application* gTheApplication;
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::Service - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- Service::Service( char* pSrvName )
- {
- if( pSrvName != nil )
- {
- fSrvNameText = new char[ strlen( pSrvName ) + 1 ];
- if( fSrvNameText != nil )
- {
- strcpy( fSrvNameText, pSrvName );
- }
- }
- else
- {
- fSrvNameText = nil;
- }
-
- /*SBR Hacked this in 10/16/94 */
- //———— Set the custom timeout interval to the default timeout
- SetTimeOutInterval( this->GetDefaultTimeOutInterval() );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::~Service - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- Service::~Service()
- {
- if( fSrvNameText != nil )
- {
- delete fSrvNameText;
- }
- }
-
-
- /*SBR Hacked this in 10/16/94 */
- //—————————————————————————————————————————————————————————————————————————————————————
- // ThreadedService::ThreadedService - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- ThreadedService::ThreadedService( char* pSrvName ):Service( pSrvName )
- {
-
- }
-
- /*SBR Hacked this in 10/16/94 */
- //—————————————————————————————————————————————————————————————————————————————————————
- // ThreadedService::~ThreadedService - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- ThreadedService::~ThreadedService()
- {
- }
-
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::CanDoService - return true if we can handle the Service indicated.
- //—————————————————————————————————————————————————————————————————————————————————————
- Boolean
- Service::CanDoService( char* pServiceName )
- {
- short tResult;
-
- if( fSrvNameText != nil )
- {
- tResult = relstring( pServiceName, fSrvNameText, false, true );
- if( tResult == 0 )
- {
- return true;
- }
- }
- return false;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::ProcessRequest - implement the Request from V.U.
- //
- // This method is called by the Dispatcher to implement the requested service.
- // Override this method within your own subclass to implement the Service.
- // Be sure to call CheckForCancel() frequently to allow for canceling.
- // If CheckForCancel() returns true, then stop processing, and return
- // immediately. Otherwise continue processing.
- //
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- Service::ProcessRequest( Request* pReq )
- {
- //———— We do nothing but beep thrice
- SysBeep( 1 );
- SysBeep( 1 );
- SysBeep( 1 );
-
- //———— and return the word 'beeped' to the script
- pReq->SetReturnValue( "beeped" );
-
- //———— no errors encountered
- return noErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::GetServiceNameText
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- Service::GetServiceNameText()
- {
- VUString* tTextVal;
-
- tTextVal = new VUString( fSrvNameText );
-
- return tTextVal;
- }
-
-
- /*SBR Hacked this in 10/16/94 */
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::CheckForCancel - Check for cancel requests and return true
- // if the current request has been canceled
- //—————————————————————————————————————————————————————————————————————————————————————
- Boolean
- Service::CheckForCancel( Request* pReq, Boolean pYield )
- {
- //———— if we have received a Cancel message for this request, cancel immediately
- if( pReq->HasBeenCanceled() )
- return true;
-
- //———— If we have not received a Cancel message, give time to other processes
- //———— unless the caller passes false in the pYield parameter.
- if( pYield )
- {
- if( !gTheApplication->IsThreaded() )
- {
- //———— Threads are not present, just call the Event Manager.
- //———— Spin the cursor so Humans can see the cancel happening.
- //———— SpinTheCursor() includes a call to Application::DoEvent()
- gTheApplication->SpinTheCursor();
-
- //———— reset any Apple event timeout counters which need it
- gTheRequestDispatcher->ResetAllTimeOutCounters();
- }
- else if( pReq->IsThreaded() )
- {
- //———— Request is threaded, yield the most efficient way.
- YieldToAnyThread( );
- }
- else
- {
- //———— Request is non-threaded, but application is threaded.
- //———— RequestDispatcher thread executes all non-threaded services.
- //———— Always yield to app to keep threaded services from getting
- //———— time, while allowing cancel and other events to be handled.
- //———— The application *always* yields to the RequestDispatcher.
- YieldToThread( kApplicationThreadID );
- }
- }
-
- return false;
- }
-
-
- /*SBR Hacked this in 10/16/94 */
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::SetTimeOutInterval - Specify a custom timeout interval in seconds.
- // You can call this within Service::ProcessRequest().
- // Must be > kResetSendThreshold ( see AERequest.cp ).
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- Service::SetTimeOutInterval( long pNewTimeOutInterval )
- {
- fTimeOutInterval = pNewTimeOutInterval;
- }
-
- /*SBR Hacked this in 10/16/94 */
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::GetTimeOutInterval - Read the current time out interval for a service.
- // Used by RequestDiapatcher::ResetAllTimeOutCounters().
- //—————————————————————————————————————————————————————————————————————————————————————
- long
- Service::GetTimeOutInterval( )
- {
- return fTimeOutInterval;
- };
-
- /*SBR Hacked this in 10/16/94 */
- //—————————————————————————————————————————————————————————————————————————————————————
- // Service::GetDefaultTimeOutInterval - Overload this in your service object to specify
- // a custom default timeout interval in seconds ( >10 ).
- //—————————————————————————————————————————————————————————————————————————————————————
- long
- Service::GetDefaultTimeOutInterval( )
- {
- // use a different constant in your Service object definition
- return kDefaultTimeOutSeconds;
- };
-
-